<?php
/**
 * 商品活动专区
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class PromotionRegion extends Model
{
    //set talbename
    protected $table = 'promotion_region';
    //set primary key
    protected $pk = 'rid';

    /**
     * @desc 增加商品活动专区
     * @return false|int
     */
    public function addPromotionRegion($param)
    {
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    /**
     * @desc 获取商品活动专区
     * @param $rid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getPromotionRegion($rid)
    {
        $conds = array(
            'rid' => $rid,
        );
        $field = '*';
        return Db::table($this->table)->where($conds)->field($field)->select();
    }

    /**
     * @desc 根据条件获取商品活动专区
     * @param $rid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getPromotionRegionByConds($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return $this->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    /**
     * @return \think\paginator\Collection
     * 包含分页信息
     */
    public function getPromotionRegionList()
    {
        return $this->paginate(10);
    }

    public function getParentRegion($param = 0)
    {
        $fields = ['rid', 'name',];
        $conds = [
            ['parent_id' => ['=', '0']],
            ['parent_id' => ['<>', '0']],
        ];
        return $this->where($conds[$param])->field($fields)->select();
    }

    public function getRegionOrder()
    {
        //Db::table($this->table)->
        return Db::query("select * from  promotion_region RIGHT JOIN promotion_region_goods on  promotion_region.rid=promotion_region_goods.rid where promotion_region.parent_id <> 0");
    }

    /**
     * @param $rid
     * @return int
     * @throws \think\Exception
     * 删除分区 后台管理使用
     */
    public function deleteRegion($rid)
    {
        $conds = [
            'rid' => $rid
        ];
        return $this->where($conds)->delete();
    }

    /**
     * @param $param
     * @return false|int
     */
    public function editRegion($param)
    {
        $conds = array(
            'rid' => $param['rid'],
        );
        $field = array();
        foreach ($param as $key => $value) {
            if ($key == 'rid') {
                continue;
            }
            $field[$key] = $value;
        }
        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }


    public function getEndTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function getStartTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function getIsOpenAttr($value)
    {
        $status = [
            '1' => '开',
            '0' => '关',
        ];
        return $status[$value];
    }
}
